package com.oa.srms.apps.actions.goods;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.goods.IGoodCategoryService;
import com.oa.srms.apps.business.goods.IGoodForCategoryService;
import com.oa.srms.apps.business.goods.IGoodInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.goods.GoodCategoryValue;
import com.oa.srms.apps.entity.goods.GoodForCategoryValue;
import com.oa.srms.apps.entity.goods.GoodInfoValue;

@SuppressWarnings("serial")
public class GoodForCategoryAction extends BasicAction {
	
	@Autowired
	private IGoodInfoService goodInfoService;
	@Autowired
	private IGoodCategoryService goodCategoryService;
	@Autowired
	private IGoodForCategoryService goodForCategoryService;
	
	private List<GoodCategoryValue> goodCategoryList;
	private List<GoodInfoValue> goodInfoList;
	
	private GoodForCategoryValue goodForCategoryValue;
	
	private String str;
	private String categoryId;
	private String goodIds;
	
	public String toShow() throws Exception{
		goodCategoryList= goodCategoryService.quaryGoodCategoryValue(IDEDStates.ENABLE);
		goodInfoList = goodInfoService.queryGoodInfo(IDEDStates.ENABLE);
		int index = 1;
		str = "";
		for(GoodInfoValue goodInfo : goodInfoList){
			str+="<input name='goodInfos' id='cb_goodInfo_"+goodInfo.getId()+"' class='cb_goodInfo' type='checkbox' value='"+goodInfo.getId()+"'>"+goodInfo.getGoodName()+" <font color='blue'>编码：</font>"+goodInfo.getGoodNo()+"，<font color='red'>现有数量：</font>"+goodInfo.getGoodNum()+"&emsp;&emsp;";
			if(index % 5 == 0){
				str+="<br/>";
			}
			index++;
		}
		return "toShowPage";
	}
	
	public String queryGoodInfoByCategory() throws Exception{
		goodForCategoryValue = goodForCategoryService.queryByCategoryId(Integer.parseInt(categoryId));
		String goodIds = goodForCategoryValue.getGoodIds();
		String[] goodIdArr = goodIds.split(",");
		JSONArray obj = JSONArray.fromObject(goodIdArr);
		String str = obj.toString();
		response.setContentType("text/html; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().print(str);
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String queryGoodInfoStrByCategory() throws Exception{
		response.setContentType("text/html; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().print(this.getGoodNameByCategory(Integer.parseInt(categoryId)));
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected String getGoodNameByCategory(Integer categoryId) throws Exception{
		GoodInfoValue goodInfo = null;
		goodForCategoryValue = goodForCategoryService.queryByCategoryId(categoryId);
		String goodIds = goodForCategoryValue.getGoodIds();
		String[] goodIdArr = goodIds.split(",");
		String goodInfoStr = "";
		for(String goodId : goodIdArr){
			goodInfo = goodInfoService.queryById(Integer.parseInt(goodId));
			goodInfoStr += goodInfo.getGoodName()+", ";
		}
		return goodInfoStr.substring(0, goodInfoStr.length()-2);
	}
	
	public String saveOrUpdate() throws Exception{
		boolean result = goodForCategoryService.addOrModifyGoodForCategory(Integer.parseInt(categoryId), goodIds);
		if(result){
			str="1";
		}else{
			str="0";
		}
		response.setContentType("text/html; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().print(str);
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<GoodCategoryValue> getGoodCategoryList() {
		return goodCategoryList;
	}

	public void setGoodCategoryList(List<GoodCategoryValue> goodCategoryList) {
		this.goodCategoryList = goodCategoryList;
	}

	public List<GoodInfoValue> getGoodInfoList() {
		return goodInfoList;
	}

	public void setGoodInfoList(List<GoodInfoValue> goodInfoList) {
		this.goodInfoList = goodInfoList;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public String getGoodIds() {
		return goodIds;
	}

	public void setGoodIds(String goodIds) {
		this.goodIds = goodIds;
	}

	
	
	
}
